<?php
/**
 * 基础模型类
 *
 * @access public
 * @author Sun Bin <virowin@gmail.com>
 * @version 1.0 19:25:18
 * @package model
 */

class Data_Base{
    /**
     * 表名
     *
     * @var string
     */
    
    static  $table;
    /**
     * 主键
     * 
     * @var  int
     */
    static  $primaryKey;
    /**
     * 软删除标记
     *
     * @var int
     */
    static $delflag;
    /**
     * 数据库接口
     *
     * @var obj
     */
    static $pdo;
    
	/**
     * SVIP数据库接口
     *
     * @var obj
     */
    static $svip;
    
    /**
     * 当前用户
     * @var obj
     */
    protected $cur_user = null;
    
    /**
     * 从何处跳转过来
     * @var string
     */
    public $http_referer = '';
    
    /**
     * 构造函数
     * 
     * @access public
     * @return void
     */
    public function __construct(){
        self::init_pdo();
    }
    
    /**
     * 连接数据库
     * 
     * @access public
     * @return void
     */
    public static function init_pdo(){
    	self::$pdo = &Lib_Pool::get_connect(Conf::$db);
    }
    
    /**
     * 通用SQL函数
     * 
     * @access public
     * @param $sql sql语句
     * @return array
     */
    public static function get_sql($sql){
        self::init_pdo();
        $params = array();
        $result = self::$pdo->select($sql);
        return $result;
    }
    /**
     * 通用SQL INSERT函数
     * 
     * @access public
     * @param $sql sql语句
     * @return array
     */
    public static function insert_sql($sql){
        self::init_pdo();
        $params = array();
        $result = self::$pdo->insert($sql);
        return $result;
    }
    
    /**
     * 增加一条数据
     * 
     * @access public
     * @param $array 数组
     * @return array
     */
    public static function add_one($array){
        $sql = sprintf("insert into `%s` set ",self::$table);
        foreach ($array as $key=>$row){
			$sql.=sprintf("`%s`='%s',",$key,addslashes($row));
        };
        $sql = rtrim($sql,',');
//        exit(var_dump($sql));
//		var_dump($sql);
        $result = self::$pdo->insert($sql);
        return $result;
    }
    
    /**
     * 更新一条数据
     * 
     * @access public
     * @param $array 数据
     * @param $id 主键值 
     * @return array
     */
    public static function update($set,$id){
        $sql = sprintf("UPDATE `%s` set ",self::$table);
        foreach ($set as $key=>$row){
            $sql.=sprintf("`%s`='%s',",$key,addslashes($row));
        };
        $sql = rtrim($sql,',');
        $sql.= sprintf(" WHERE `%s` = '%s'",self::$primaryKey,$id);
        $result = self::$pdo->update($sql);
        return $result;
    }
    
    /**
     * 更新一组数据
     * 
     * @access public
     * @param $set 数据
     * @param $where 限定条件 
     * @return array
     */
    public static function updatequery($set,$where){
        $sql = sprintf("UPDATE `%s` set ",self::$table);
        foreach ($set as $key=>$row){
            $sql.=sprintf("`%s`='%s',",$key,addslashes($row));
        };
        $sql = rtrim($sql,',');
        $sql_tmp = '';    
        foreach ($where as $key => $val){        	
        	$sql_tmp .= $sql_tmp == '' ? sprintf(" WHERE 1=1 AND `%s` = '%s'",$key,$val) : sprintf(" AND `%s` = '%s'",$key,$val);
        }
        $sql .= $sql_tmp;

        $result = self::$pdo->update($sql);
        return $result;
    }
    
    /**
     * 通用使用WHERE查询方法
     * 
     * @access public
     * @param $where where条件
     * @return array
     */
    public static function get_list_where($where = null){
        self::init_pdo();
        if($where != null)
        $sql = sprintf("select * from `%s` where %s",self::$table,$where);
        else
        $sql = sprintf("select * from `%s`",self::$table);
        $result = self::$pdo->select($sql);
        return $result;
    }
    /**
     * 通用使用WHERE获取count方法
     * 
     * @access public
     * @param $where where条件
     * @return array
     */
    public static function get_list_total_where($where = null){
        if($where != null)
        $sql = sprintf("select count(*) as num from `%s` where %s",self::$table,$where);
        else
        $sql = sprintf("select count(*) as num from `%s`",self::$table);
//        echo $sql;
        $result = self::$pdo->select($sql);
        return $result;
    }
    
    /**
     * 通用使用filter获取数据
     * 
     * @access public
     * @param $filter 键值数组
     * @param $filter_value 值
     * @return array
     */
    public static function get_list($filter = '',$filter_value = ''){
        $sql = sprintf("select * from `%s` where 1=1 ",self::$table);
        if ($filter && $filter_value) {
            $sql .= sprintf(" and `%s`='%s'",$filter,addslashes($filter_value));
        }
        $params = array();
        $result = self::$pdo->select($sql);
        return $result;
    }
    /**
     * 通用使用filter获取count
     * 
     * @access public
     * @param $filter 键值数组
     * @param $filter_value 值
     * @return array
     */
    public static function get_list_total($filter='',$filter_value=''){
        $sql = sprintf("select count(*) as num from `%s` where 1 ",self::$table);
        if ($filter && $filter_value) {
            $sql .= sprintf(" and `%s`='%s'",$filter,addslashes($filter_value));
        }
        $params = array();
        $result = self::$pdo->select($sql);
        return $result;
    }
    
    /**
	 *
 	 * 根据ID获取PRODUCT
	 * @param  $ids array,str,int格式
	 */
    public static function get_by_id( $ids = "" ,$where = null){
        self::init_pdo();
        $sql = sprintf("select * from `%s` ",self::$table);
        if(!$ids) return null;
        if (is_array($ids)){
            $ids = implode(",",$ids);
            $sql .= sprintf(" WHERE `%s` in ( %s) %s",self::$primaryKey,$ids,$where);
        }elseif(is_numeric($ids)) {
            $sql .= sprintf(" WHERE `%s` ='%d' %s",self::$primaryKey,$ids,$where);
        }elseif(preg_match("/^[0-9,]*$/si",$ids)){
            $ids = addslashes($ids);
            $sql .= sprintf(" WHERE `%s` in ( %s) %s",self::$primaryKey,$ids,$where);
        }
        //break point
        $params = array();
        $result = self::$pdo->select($sql);
        return $result['result'];
    }
}
